import os
import subprocess
import sys

from defines import *


def main():
    core_path = os.path.join(MAIN_PATH, r'rtl/core')
    utils_path = os.path.join(MAIN_PATH, r'rtl/utils')
    soc_path = os.path.join(MAIN_PATH, r'rtl/soc')
    tb_path = os.path.join(MAIN_PATH, r'sim/tb')

    # iverilog 程序
    iverilog_cmd = ['iverilog']
    # 支持 SystemVerilog
    iverilog_cmd += ['-g2005-sv']
    # 编译生成文件
    iverilog_cmd += ['-o', OUT_FILE_PATH]
    # 确定头文件路径
    iverilog_cmd += ['-I', core_path]
    # tb 文件
    iverilog_cmd.append(tb_path + r'/tb.sv')
    # core 文件夹
    iverilog_cmd.append(core_path + r'/ex.v')
    iverilog_cmd.append(core_path + r'/id.v')
    iverilog_cmd.append(core_path + r'/id_ex.v')
    iverilog_cmd.append(core_path + r'/instf.v')
    iverilog_cmd.append(core_path + r'/instf_id.v')
    iverilog_cmd.append(core_path + r'/LilyRiscv.v')
    iverilog_cmd.append(core_path + r'/pc_reg.v')
    iverilog_cmd.append(core_path + r'/regs.v')
    # soc 文件夹
    iverilog_cmd.append(soc_path + r'/LilyRiscv_top.v')
    iverilog_cmd.append(soc_path + r'/rom.v')
    # utils 文件夹
    iverilog_cmd.append(utils_path + r'/pipe_dff.v')

    process = subprocess.Popen(iverilog_cmd)
    process.wait(timeout=5)


if __name__ == '__main__':
    sys.exit(main())